﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.UI.TypeExtensions;

namespace Web.UI.AdminLTE.Common
{
    public abstract class HtmlElementBase : IHtmlString
    {
        protected IDictionary<string, object> HtmlAttr;

        protected HtmlElementBase()
        {
            HtmlAttr = new Dictionary<string, object>();
        }

        protected void SetId(string id)
        {
            HtmlAttr.AddOrReplace("id", id);
        }

        protected void EnsureClass(params string[] classNames)
        {
            foreach (var className in classNames)
            {
                if (HtmlAttr.ContainsKey("class"))
                {
                    var text = HtmlAttr["class"].ToString();
                    if (!text.Contains(className))
                    {
                        HtmlAttr["class"] += " " + className;
                    }
                }
                else
                {
                    MergeHtmlAttribute("class", className);
                }
            }
        }

        protected void EnsureClassRemoval(string className)
        {
            if (!HtmlAttr.ContainsKey("class")) return;

            var list = HtmlAttr["class"]
                .ToString().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                .ToList();
            list.Remove(className);
            HtmlAttr["class"] = string.Join(" ", list);
        }

        protected void EnsureHtmlAttribute(string key, string value, bool replaceExisting = true)
        {
            if (!HtmlAttr.ContainsKey(key))
            {
                HtmlAttr.Add(key, value);
                return;
            }
            if (replaceExisting)
            {
                HtmlAttr[key] = value;
                return;
            }
            HtmlAttr[key] += " " + value;
        }

        protected void SetHtmlAttributes(IDictionary<string, object> htmlAttributes)
        {
            HtmlAttr.MergeHtmlAttributes(htmlAttributes);
        }

        protected void SetHtmlAttributes(object htmlAttributes)
        {
            SetHtmlAttributes(htmlAttributes.ToDictionary().FormatHtmlAttributes());
        }

        protected void MergeHtmlAttribute(string key, string value)
        {
            if (HtmlAttr != null)
            {
                if (HtmlAttr.ContainsKey(key))
                {
                    HtmlAttr[key] = value;
                }
                else
                {
                    HtmlAttr.Add(key, value);
                }
            }
            else
            {
                HtmlAttr = new Dictionary<string, object>();
                HtmlAttr.Add(key, value);
            }
        }

        protected void MergeHtmlAttributes(object htmlAttributes, string appendToKey = null)
        {
            var dictionary = htmlAttributes.ToDictionary();
            foreach (var current in dictionary)
            {
                MergeHtmlAttribute(appendToKey + current.Key, current.Value.ToString());
            }
        }

        protected void MergeHtmlDataAttributes(object htmlAttributes)
        {
            MergeHtmlAttributes(htmlAttributes, "data-");
        }

        public abstract string ToHtmlString();

        public override string ToString()
        {
            return ToHtmlString();
        }
    }
}